<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: User.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


/*
 * SELECT * FROM store_questions a WHERE a.qid=121 OR a.parent_qid=121;
SELECT * FROM store_question_attributes d WHERE d.qid=121;
SELECT * FROM store_answers d WHERE d.qid=121;
SELECT * FROM store_survey_37628 b WHERE b.;

SELECT * FROM store_tokens_37628 e WHERE e.
;
SELECT * FROM store_survey_37628 b JOIN store_tokens_37628 c ON b.token=c.token


SELECT * FROM store_quota
*/
//class Permission extends Baogg_Db_Table {
class Qa_Questions  extends Baogg_Db_Table_Qa {
	/**
	 * The default table name
	 */
	protected $_name;
	protected $_db ;
	protected $_primary;
	public static $dict_select;

	function __construct() {
		$this->_name =   'questions';

		$this->primary= $this->_primary='qid';
		parent::__construct();
	}

	/**
	 *
	 * Enter description here ...
	 * @param string,blank or multi $type
	 */
	public static function genOp($name,$selected='',$type=''){
		$ret='';


		$arr_op=array();
		if(!$type){
			$arr_op=array('=','!=','>','<', '>=','<=','between','like','in','not in','is null','is not null');
		}else if($type=='multi'){
			$arr_op=array('in','not in');
		}else if($type=='check'){
			$arr_op=array('=','!=');
		}

		$arr_op_label=array_map(array('Baogg_Language','get'),$arr_op);

		$arr_selected=array_fill(0,count($arr_op),'');
		$selected_index=array_search($selected,$arr_op);
		if($selected_index!==false){
			$arr_selected[$selected_index]='selected="selected"';
		}

		$arr_opt=array_fill(0,count($arr_op),'<option value="%s" %s>%s</option>');
		$ret.="<select name=\"field_op[{$name}]\">";
		$ret.=implode('',array_map("sprintf",$arr_opt,$arr_op,$arr_selected,$arr_op_label));
		$ret.="</select>";
		return $ret;
	}
	public static function genAttr($field_name,$arr_value=array()){
		global $LANG;
		/* $Rule=new Rule();
		$rs_rule=$Rule->getList(array('state'=>1,'rule_type_id'=>Rule::CMH_TYPE),array('sort'=>'ordering','dir'=>'asc'),array(),array('rule_id','rule_name'));
		 */
		
		$output='';
		$output.= "<td class='col5'><input type='text' name='field_title[$field_name]' value='".@$arr_value['qa_field_name']."' /></td> ";
		$output.= "<td class='col6'><input type='text' name='field_order[$field_name]' value='".@$arr_value['ordering']."' /></td> ";
		

		
	 	//$output.= "<td class='col8'><input type='checkbox' name='field_is_list[$field_name]' value='1' ".(@$arr_value['is_list']?" checked='checked'":"")." /></td> ";
		$output.= "<td class='col8'>&nbsp</td>";
	 	
	 	$output.= "<td class='col9'><select name='field_dict_id[$field_name]'  name='field_dict_id[$field_name]' ><option value=''>{$LANG['msg_select_item']}</option>".self::genDictSelect(@$arr_value['dict_id'])."</select></td> ";
	 	
	/*	$output.= "<td class='col9'><input type='checkbox' name='field_is_chart[$field_name]' value='1' ".(@$arr_value['is_chart']?" checked='checked'":"")." /></td> ";
		$output.= "<td class='col7'><input type='checkbox' name='field_is_filter[$field_name]' value='1' ".(@$arr_value['is_form']?" checked='checked'":"")." /></td> ";
		$output.= "<td class='col10'><input type='checkbox' name='field_is_sort[$field_name]' value='1' ".(@$arr_value['is_sort']?" checked='checked'":"")." /></td> ";
		 */
		
		return $output;
	}
	
	public static function genDictSelect($dict_id=0){
		$ret='';
		if(!isset(self::$dict_select)){
			$Dict=new Dict();		
			self::$dict_select=$Dict->getCombo(array('state'=>1),array('ordering'=>'asc'),null,array('dict_id','dict_name','dict_pid'));	
		}		
		foreach((array)self::$dict_select as $v){
			$selected=($v[0]==$dict_id?'  selected="selected" ':'');
			$ret.= "<option value='{$v[0]}' {$selected}>{$v[1]}</option>";
		}			
		
		return $ret;
	}

	public function genFieldMap($survey_id){
		$Qa_Surveys=new Qa_Surveys();
		$rs_qa_survey=$Qa_Surveys->getOne($survey_id);
		$survey_id=@$rs_qa_survey[0]['sid'];
		$language=@$rs_qa_survey[0]['language'];
		
		$Qa_Groups=new Qa_Groups();
		$Qa_Questions=new Qa_Questions();
		
		
		//array(array('name'=>'questions','cols'=>array('*'), 'condition'=>'groups.gid=questions.gid and questions.LANGUAGE=\'zh-Hans\' and parent_qid=0'))
		$rs_qa_groups=$Qa_Groups->getList(array('language'=>$language,'sid'=>(int)$survey_id),array('sort'=>'group_order','dir'=>'asc'),null,array('gid','group_name','group_order'));
		
		
		$fieldmap=array();
		$fieldmap["id"]=array("fieldname"=>"id", 'sid'=>$survey_id, 'type'=>"id", "gid"=>"", "qid"=>"", "aid"=>"");
		$fieldmap["submitdate"]=array("fieldname"=>"submitdate", 'type'=>"submitdate", 'sid'=>$survey_id, "gid"=>"", "qid"=>"", "aid"=>"");
		foreach((array)$rs_qa_groups as $v_group){			
			$rs_qa_questions=$Qa_Questions->getList(array('gid'=>(int)$v_group['gid'],'parent_qid'=>0,'LANGUAGE'=>$language),array('sort'=>'question_order','dir'=>'asc'));
			foreach($rs_qa_questions as $v){
				$fieldmap+=$this->genFieldMapByQuestion($v['qid']);
			}
		}
		return $fieldmap;
	}

	public function genFieldMapByQuestion($question_id){
		$rs=$this->getOne($question_id);
		$v=@$rs[0];
		
		Zend_Loader::loadFile('question_attribute.php','config/qa/',true);
		$qtypes=getqtypelist();
		
		$Qa_Surveys=new Qa_Surveys();
		$rs_qa_survey=$Qa_Surveys->getOne($v['sid']);
		$survey_id=@$rs_qa_survey[0]['sid'];
		$language=@$rs_qa_survey[0]['language'];
		
		$fieldmap=array();
		/* if(!isset($qtypes[$v['type']])){
			echo __FILE__.__LINE__.'<pre>';var_dump($v['type']);exit;
		} */
		if ($qtypes[$v['type']]['subquestions']==0 && $v['type'] != "R"){
			$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}";
			$fieldmap[$fieldname]=array('fieldname'=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>"");
			switch($v['type'])
			{
				case "L":  //RADIO LIST
				case "!":  //DROPDOWN LIST
					if ($v['other'] == "Y")
					{
						$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}other";
						$fieldmap[$fieldname]=array("fieldname"=>$fieldname,
															'type'=>$v['type'],
												'sid'=>$v['sid'],
												"gid"=>$v['gid'],
												"qid"=>$v['qid'],
												"aid"=>"other");
					}
					break;
				case "O": //DROPDOWN LIST WITH COMMENT
					$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}comment";
					$fieldmap[$fieldname]=array("fieldname"=>$fieldname,
												'type'=>$v['type'],
												'sid'=>$v['sid'],
												"gid"=>$v['gid'],
												"qid"=>$v['qid'],
												"aid"=>"comment");
					break;
			}
		}
		// For Multi flexi question types
		elseif ($qtypes[$v['type']]['subquestions']==2 && $qtypes[$v['type']]['answerscales']==0)
		{
			$rs_sub_question=$this->getList(array('parent_qid'=>(int)$v['qid'],'language'=>$language),array('sort'=>'question_order','dir'=>'asc'));

			//Now first process scale=1
			$rs_sub_question_y=array();
			foreach ($rs_sub_question as $k_sub=>$v_sub)
			{
				if($v_sub['scale_id']==1) {
					$rs_sub_question_y[]=$v_sub;
					unset($rs_sub_question[$k_sub]);
				}
			}

			reset($rs_sub_question);
			foreach ($rs_sub_question as $v_sub)
			{
				foreach($rs_sub_question_y as $v_sub_y)
				{
					$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}{$v_sub['title']}_{$v_sub_y['title']}";
					$fieldmap[$fieldname]=array("fieldname"=>$fieldname,
												'type'=>$v['type'],
												'sid'=>$v['sid'],
												"gid"=>$v['gid'],
												"qid"=>$v['qid'],
												"aid"=>$v_sub['title']."_".$v_sub_y['title'],
												"sqid"=>$v_sub['qid']);
				}
			}
		}
		elseif ($qtypes[$v['type']]['answerscales']==2  && $qtypes[$v['type']]['subquestions']==1)//$v['type'] == "1"
		{
			$rs_sub_question=$this->getList(array('parent_qid'=>(int)$v['qid'],'language'=>$language),array('sort'=>'question_order','dir'=>'asc'));
			foreach ($rs_sub_question as $v_sub)
			{
				$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}{$v_sub['title']}#0";
				$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>$v_sub['title'], "scale_id"=>0);
				$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}{$v_sub['title']}#1";
				$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>$v_sub['title'], "scale_id"=>1);

			}
		}
		elseif ($v['type'] == "R")
		{
			//MULTI ENTRY
			$rs_answer=$Qa_Answers->getList(array('qid'=>(int)$v['qid'],'language'=>$language),array('sortorder','code'));
			$count=count($rs_answer);

			for ($i=1; $i<=$count; $i++)
			{
				$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}$i";
				$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>$i);
			}

		}
		else{
			$rs_sub_question=$this->getList(array('parent_qid'=>(int)$v['qid'],'language'=>$language),array('sort'=>'question_order','dir'=>'asc'));
			foreach ($rs_sub_question as $v_sub)
			{
				$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}{$v_sub['title']}";
				$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>$v_sub['title']);
				if ($v['type'] == "P")
				{
					$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}{$v_sub['title']}comment";
					$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>$v_sub['title']."comment");
				}
			}
			if ($v['other']=="Y" && ($v['type']=="M" || $v['type']=="P"))
			{
				$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}other";
				$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>"other");
				
				if ($v['type']=="P")
				{
					$fieldname="{$v['sid']}X{$v['gid']}X{$v['qid']}othercomment";					
					$fieldmap[$fieldname]=array("fieldname"=>$fieldname, 'type'=>$v['type'], 'sid'=>$v['sid'], "gid"=>$v['gid'], "qid"=>$v['qid'], "aid"=>"othercomment");
				}
			}
		}
		
		return $fieldmap;
	}
	
	public static function getOps(){
		$arr_op=array('=','!=','>','<', '>=','<=','between','like','in','not in','is null','is not null');
		$arr_op_label=array_map(array('Baogg_Language','get'),$arr_op);
		return array_combine($arr_op,$arr_op_label);
		
	}
	
	
	
}